package com.zisuyi.uilibrary.module.dialog.rating

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.fragment.app.FragmentActivity
import com.weilele.mvvm.utils.activity.getResBool
import com.zisuyi.uilibrary.R
import com.zisuyi.uilibrary.databinding.DialogRateUsTipBinding
import com.zisuyi.uilibrary.module.dialog.BaseTitleDialog
import com.zisuyi.uilibrary.uitils.GooglePlayUtil
import java.lang.Exception


class RatingDialog : BaseTitleDialog<DialogRateUsTipBinding>() {
    companion object {
        fun show(activity: FragmentActivity?) {
            RatingDialog().show(activity)
        }

        /**
         * 跳转到google应用市场
         *
         * @param context
         */
        fun goGooglePlay(context: Context?) {
            context ?: return
            val packageName = context.packageName
            try {
                when {
                    getResBool(R.bool.enable_google_store) == true -> {
                        GooglePlayUtil.openAppInPlay(context, packageName)
                    }
                    getResBool(R.bool.enable_huawei_store) == true -> {
                        GooglePlayUtil.launchHuawei(context, packageName)
                    }
                    else -> {
                        GooglePlayUtil.launchAppDetail(context, packageName, null)
                    }
                }
            } catch (exception: Exception) {
                context.startActivity(
                    Intent(
                        Intent.ACTION_VIEW,
                        Uri.parse("https://play.google.com/store/apps/details?id=$packageName")
                    )
                )
            }
        }
    }

    //游戏次数进度适配器
    private val ratingBarAdapter = RatingBarAdapter()


    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.ratingRcv.adapter = ratingBarAdapter
        mBinding.ratingRcv.suppressLayout(true)
        ratingBarAdapter.setMax(5, R.drawable.rate_star, R.drawable.rate_star_grey)
        ratingBarAdapter.setRating(4)
        setTitle(R.string.rate_us)
    }

    override fun getClickView(): List<View?>? {
        return listOf(mBinding.ratingBt)
    }


    override fun onSingleClick(view: View) {
        super.onSingleClick(view)
        when (view) {
            mBinding.ratingBt -> {
                goGooglePlay(context)
                dismiss()
            }
            else -> {
            }
        }
    }


}